package com.xinghai.service.impl;


import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.github.yulichang.base.MPJBaseServiceImpl;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import com.xinghai.common.constant.GenConstants;
import com.xinghai.mapper.JkqActivityMapper;
import com.xinghai.domain.JkqActivity;
import com.xinghai.service.IJkqActivityService;

/**
 * 活动管理Service业务层处理
 *
 * @author xinghai
 * @date 2025-08-02
 */
@Service
public class JkqActivityServiceImpl extends MPJBaseServiceImpl<JkqActivityMapper, JkqActivity>
        implements IJkqActivityService
{

    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    /**
     * 查询活动管理列表
     *
     * @param jkqActivity 活动管理
     * @return 活动管理集合
     */
    @Override
    public List<JkqActivity> list(JkqActivity jkqActivity) {
        QueryWrapper<JkqActivity> queryWrapper = new QueryWrapper<>();
        if (jkqActivity.getTitle()!= null  && !jkqActivity.getTitle().isEmpty()){
            queryWrapper.like("title", jkqActivity.getTitle());
        }
        if (jkqActivity.getType()!= null  && !jkqActivity.getType().isEmpty()){
            queryWrapper.eq("type", jkqActivity.getType());
        }
        if (jkqActivity.getStatus()!= null  && !jkqActivity.getStatus().isEmpty()){
            queryWrapper.eq("status", jkqActivity.getStatus());
        }
        if (jkqActivity.getParams().get("beginCreateTime")!= null && !"".equals(jkqActivity.getParams().get("beginCreateTime"))
        &&jkqActivity.getParams().get("endCreateTime")!= null && !"".equals(jkqActivity.getParams().get("endCreateTime"))){
            queryWrapper.between("create_time", jkqActivity.getParams().get("beginCreateTime"),jkqActivity.getParams().get("endCreateTime"));
        }
        return this.list(queryWrapper);
    }

    /**
     * 导入活动管理信息
     *
     * @param list 活动管理信息集合
     * @return 结果
     */
    @Override
    @Transactional
    public boolean importData(List<JkqActivity> list){
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        try {
            JkqActivityMapper jkqActivityMapper = sqlSession.getMapper(JkqActivityMapper.class);
            list.forEach(jkqActivityMapper::insert);
            // 提交数据
            sqlSession.commit();
            sqlSession.rollback();
        } catch (Exception e) {
            sqlSession.rollback();
            return false;
        } finally {
            sqlSession.close();
        }
        return true;
    }
}
